﻿using System;
using BP.DA;
using BP.Sys;
using BP.Difference;
using Newtonsoft.Json.Linq;

namespace BP.CCFast
{
    /// <summary>
    /// 页面功能实体
    /// </summary>
    public class HttpHandlerDataBank : BP.WF.HttpHandler.DirectoryPageBase
    {
        /// <summary>
        /// 是不是开发状态？
        /// </summary>
        /// <returns></returns>
        public bool IsDev()
        {
            if (SystemConfig.HostURL.ToLower().Contains("http://localhost") == true)
                return true;
            return false;
        }
        public HttpHandlerDataBank()
        {

        }
        /// <summary>
        /// 获得字符串
        /// </summary>
        /// <returns></returns>
        public string GenerString()
        {
            string mark = GetRequestVal("Mark");
            if (mark == null)
                return "";

            if (mark.StartsWith("DBSrc.") == false)
                return "不符合预期的格式.";

            string[] strs = mark.Split('.');
            string flag = strs[0];
            string dbSrcNo = strs[1];
            string type = strs[2];
            string objID = strs[3];
            string paras = strs[4].Replace("[。]", ".");

            if (type.Equals("SFTable") == true)
            {
                var table = new SFTable(objID);
                if (table.RetrieveFromDBSources() == 0)
                    return "err@不存在[" + objID + "]的查询.";

                if (DataType.IsNullOrEmpty(paras) == true)
                    return table.GenerDataOfJson();

                AtPara para = new AtPara(paras);
                return table.GenerDataOfJsonExt(para.HisHT);
            }

            if (type.Equals("Search") == true)
            {
                var search = new SFSearch(objID);
                if (search.RetrieveFromDBSources() == 0)
                    return "err@不存在[" + objID + "]的查询.";

                ////判断缓存类型.
                //if (search.KeeepModel == KeeepModel.ForEver)
                //{
                //    return "xxx";
                //}

                //if (search.KeeepModel == KeeepModel.ForDeptNo)
                //{
                //    return "xxx";
                //}

                string json = search.GenerDataOfJson(paras); //获得json.
                return json;
            }

            if (type.Equals("Proc") == true)
            {
                throw new Exception("err@没有开发proc的解析.");
                //var proc = new SFProc(objID);
                //if (proc.RetrieveFromDBSources() == 0)
                //    return "err@不存在[" + objID + "]的查询.";
                //if (DataType.IsNullOrEmpty(paras) == true)
                //    return proc.HisDBVarStr();
                //AtPara para = new AtPara(paras);
                //return table.GenerDataOfJsonExt(para.HisHT);
            }
            throw new Exception("err@标记错误:" + type);
        }
        /// <summary>
        /// 压入到数据库把表达式.
        /// </summary>
        /// <returns></returns>
        public string DataBank_Push()
        {
            //if (this.IsDev() == false)
            //    return "开发状态下不能压入.";
            string dbNo = GetRequestVal("DBNo");
            string dbName = GetRequestVal("DBName");
            string boxs = GetRequestVal("DataBoxs");
            string classID = GetRequestVal("ClassID");

            BP.CCFast.DataBank.DataBank db = new BP.CCFast.DataBank.DataBank();
            db.No = classID;
            bool isInsert = false;
            if (db.RetrieveFromDBSources() == 0)
            {
                db.SetValByKey("Doc", boxs);
                db.Insert();
                isInsert = true;
            }
            else
            {
                if (db.GetValStringByKey("Doc").Equals(boxs) == true)
                    return "无需Push.";
            }


            JArray jsonObject = JArray.Parse(boxs);
            SFTable table = new SFTable();
            SFSearch search = new SFSearch();
            SFProc proc = new SFProc();

            foreach (JObject json in jsonObject)
            {
                string no = json.GetValue("No").ToString(); //盒子编号.
                string name = json.GetValue("Name").ToString(); //盒子编号.
                string exp = json.GetValue("Exp").ToString(); //盒子编号.
                string dbType = json.GetValue("DBType").ToString(); //盒子编号.

                no = classID + "_" + no; //设置编号.

                //如果是字典》
                if (dbType.Equals("SFTable") == true)
                {
                    table.SetValByKey("No", no);
                    if (table.RetrieveFromDBSources() == 0)
                    {
                        table.SetValByKey("No", no);
                        table.SetValByKey("Name", name);
                        table.SetValByKey("SelectStatement", exp);
                        table.SetValByKey("FK_SFDBSrc", dbNo);
                        table.SrcType = "SQL";
                        table.SetPara("EnName", "TS.FrmUI.SFTableSQLNoName");
                        table.DirectInsert();
                        continue;
                    }
                    if (table.SelectStatement.Equals(exp) == true)
                        continue;

                    table.SetValByKey("Name", name);
                    table.SetValByKey("SelectStatement", exp);
                    table.SetValByKey("FK_SFDBSrc", dbNo);
                    table.SrcType = "SQL";
                    table.SetPara("EnName", "TS.FrmUI.SFTableSQLNoName");
                    table.SetPara("IsDelete", "0");
                    table.SetPara("IsDataBank", "0");
                    table.Update(); //更新.
                    continue;
                }

                //查询.
                if (dbType.Equals("Search") == true)
                {
                    //if (exp.indexOf('@') == -1 || exp.toLocaleLowerCase().indexOf('group by') == -1 || exp.toLocaleLowerCase().includes(`like`) == true)
                    //{
                    //    this.AddSearchList(no, name, exp);
                    //    return;
                    //}

                    //if (exp.toLocaleLowerCase().includes(`= '@key'`) == true || exp.toLocaleLowerCase().includes(`= '@key'`) == true)
                    //{
                    //    this.AddSearchEn(no, name, exp);
                    //    return;
                    //}

                    //其他的都是查询.
                    // this.AddSearchList(no, name, exp);
                    search.SetValByKey("No", no);
                    if (search.RetrieveFromDBSources() == 0)
                    {
                        search.SetValByKey("No", no);
                        search.SetValByKey("Name", name);
                        search.SetValByKey("SelectStatement", exp);
                        search.SetValByKey("FK_SFDBSrc", dbNo);
                        search.SetPara("EnName", "TS.FrmUI.SFSearchSQL");
                        search.DirectInsert();
                        continue;
                    }

                    if (search.SelectStatement.Equals(exp) == true)
                        continue;
                    search.SetValByKey("Name", name);
                    search.SetValByKey("SelectStatement", exp);
                    search.SetValByKey("FK_SFDBSrc", dbNo);
                    search.SetPara("EnName", "TS.FrmUI.SFSearchSQL");
                    search.Update(); //更新.
                    continue;
                }

                //过程》
                if (dbType.Equals("Proc") == true)
                {
                    proc.SetValByKey("No", no);
                    if (proc.RetrieveFromDBSources() == 0)
                    {
                        proc.SetValByKey("No", no);
                        proc.SetValByKey("Name", name);
                        proc.SetValByKey("SelectStatement", exp);
                        proc.SetValByKey("FK_SFDBSrc", dbNo);
                        proc.SetPara("EnName", "TS.FrmUI.SFProcSQL");
                        proc.DirectInsert();
                        continue;
                    }

                    if (proc.SelectStatement.Equals(exp) == true)
                        continue;
                    proc.SetValByKey("Name", name);
                    proc.SetValByKey("SelectStatement", exp);
                    proc.SetValByKey("FK_SFDBSrc", dbNo);
                    proc.SetPara("EnName", "TS.FrmUI.SFProcSQL");
                    proc.Update(); //更新.
                    continue;
                }
            }

            if (isInsert == false)
            {
                db.SetValByKey("Doc", boxs);
                db.Update();
            }
            return "压入成功";
        }
    }
}